package server.service.factories;

import sun.security.krb5.Config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class ServicesFactory {

  static Properties properties;
  static Map<Class<?>, Object> map = new ConcurrentHashMap<>();

  static {
    try (InputStream in = Config.class.getResourceAsStream("/application.properties")) {
      properties = new Properties();
      properties.load(in);
      Set<String> names = properties.stringPropertyNames();
      for (String name : names) {
        if (name.endsWith("Service")) {
          Class<?> interfaceClass = Class.forName(name);
          Class<?> instanceClass = Class.forName(properties.getProperty(name));
          map.put(interfaceClass, instanceClass.newInstance());
        }
      }
    } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
      throw new ExceptionInInitializerError(e);
    }
  }

  public static <T> T getService(Class<T> interfaceClass) {
    return (T) map.get(interfaceClass);
  }
}

